package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.OrderExceptions;
import com.example.mall.domain.ProductInventory;
import com.example.mall.dto.OrderExceptionsDto;
import com.example.mall.dto.ProductInventoryDto;
import com.example.mall.mapper.OrderExceptionsMapper;
import com.example.mall.query.OrderExceptionsQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.query.ProductInventoryQuery;
import com.example.mall.service.IOrderExceptionsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单异常处理表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-09
 */
@Service
public class OrderExceptionsServiceImpl extends ServiceImpl<OrderExceptionsMapper, OrderExceptions> implements IOrderExceptionsService {

    @Autowired
    private OrderExceptionsMapper orderExceptionsMapper;

    @Override
    public PageDTO<OrderExceptionsDto> queryPage(OrderExceptionsQuery orderExceptionsQuery) {
        if (orderExceptionsQuery == null) {
            orderExceptionsQuery = new OrderExceptionsQuery();
        }
        Page<OrderExceptions> page = orderExceptionsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<OrderExceptions> cartPage = lambdaQuery()
                .like(orderExceptionsQuery.getExceptionId()!= null, OrderExceptions::getExceptionId, orderExceptionsQuery.getExceptionId())
                .like(orderExceptionsQuery.getOrderId()!= null, OrderExceptions::getOrderId, orderExceptionsQuery.getOrderId())
                .like(orderExceptionsQuery.getExceptionType()!= null, OrderExceptions::getExceptionType, orderExceptionsQuery.getExceptionType())
                .like(orderExceptionsQuery.getHandledStatus()!= null, OrderExceptions::getHandledStatus, orderExceptionsQuery.getHandledStatus())
                .like(orderExceptionsQuery.getHandledBy()!= null, OrderExceptions::getHandledBy, orderExceptionsQuery.getHandledBy())
                .apply(orderExceptionsQuery.getHandledTime() != null,
                        "DATE_FORMAT(handled_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        orderExceptionsQuery.getHandledTime())
                .orderByDesc(OrderExceptions::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, OrderExceptionsDto.class);
    }

    @Override
    public OrderExceptions queryOrderExceptionsById(Long exceptionId) {
        AssertUtil.isNotNull(exceptionId, "订单异常ID不能为空");
        return orderExceptionsMapper.selectById(exceptionId);
    }

    @Override
    public List<OrderExceptions> queryOrderExceptionsList() {
        return this.list();
    }

    @Override
    public void update(OrderExceptionsDto orderExceptionsDto) {
        AssertUtil.isNotNull(orderExceptionsDto.getExceptionId(), "订单异常ID不能为空");
        OrderExceptions orderExceptions = this.queryOrderExceptionsById(orderExceptionsDto.getExceptionId());
        AssertUtil.isNotNull(orderExceptions, "订单异常不存在");
        CopyUtils.copyProperties(orderExceptionsDto, orderExceptions);
        orderExceptions.setUpdateDate(new Date());
        this.updateById(orderExceptions);
    }

    @Override
    public void delete(Long exceptionId) {
        AssertUtil.isNotNull(exceptionId, "订单异常ID不能为空");
        OrderExceptions orderExceptions = this.queryOrderExceptionsById(exceptionId);
        AssertUtil.isNotNull(orderExceptions, "订单异常不存在");
        orderExceptions.setHandledStatus(BaseConstants.USED_ALREADY);
        orderExceptions.setUpdateDate(new Date());
        orderExceptionsMapper.updateById(orderExceptions);
    }

    @Override
    public void deleteBatch(List<Long> exceptionIds) {
        AssertUtil.isNotNull(exceptionIds, "订单异常ID列表不能为空");
        List<OrderExceptions> orderExceptions = orderExceptionsMapper.selectBatchIds(exceptionIds);
        AssertUtil.isNotNull(orderExceptions, "订单异常不存在");
        orderExceptions.forEach(orderExceptions1 -> {
            orderExceptions1.setHandledStatus(BaseConstants.USED_ALREADY);
            orderExceptions1.setUpdateDate(new Date());
            orderExceptionsMapper.updateById(orderExceptions1);
        });
    }

    @Override
    public void insert(OrderExceptionsDto orderExceptionsDto) {
        OrderExceptions orderExceptions = new OrderExceptions();
        CopyUtils.copyProperties(orderExceptionsDto, orderExceptions);
        orderExceptions.setExceptionId(CodeGenerateUtils.generateUniqueId());
        orderExceptions.setCreateDate(new Date());
        orderExceptions.setUpdateDate(new Date());
        orderExceptions.setHandledStatus(BaseConstants.NOT_USED);
        orderExceptionsMapper.insert(orderExceptions);
    }
}
